import instance from './requset'
// 登录
export const loginApi = function (data) {
    return instance({
        method: 'post',
        url: '/user/login',
        data,
        headers: { isLoading: true },
    })
}
// 获取当前用户信息
export const getselfApi = function () {
    return instance({
        method: 'get',
        url: 'user/getself',
        headers: { isLoading: true },
    })
}
export const getmenuApi = function () {
    return instance({
        method: 'get',
        url: 'user/getmenu',
        headers: { isLoading: true },
    })
}
// 添加用户
export const userAddApi = function (data) {
    return instance.post('/user/add', data)
}
//删除用户
export const userDeleteApi = function (id) {
    return instance.get('user/delete?id=' + id)
}
// 获取用户列表
export const userGetlistApi = function (page, size) {
    return instance({
        method: 'get',
        url: `user/getlist?page=${page}&size=${size}`,
        headers: { isLoading: true },
    })
}
//获取全部有效角色
export const roleGetlistApi = function () {
    return instance({
        method: 'get',
        url: `role/getlist`,
        headers: { isLoading: true },
    })
}
// 添加角色
export const roleAddApi = function (obj) {
    return instance.post('role/add', obj)
}
export const roleDeleteApi = function (obj) {
    return instance.post('role/delete', obj)
}
export const roleEditApi = function (obj) {
    return instance.post('role/change', obj)
}
// 获取全部接口权限列表
export const rolePermissionsApi = function (page, size) {
    return instance({
        method: 'get',
        url: `role/permissions?page=${page}&size=${size}`,
        headers: { isLoading: true },
    })
}
/**
 *首页接口
 */
// 数据统计
export const indexStatsApi = function () {
    return instance({
        method: 'get',
        url: `index/stats`,
        headers: { isLoading: true },
    })
}
// 设备分布
export const indexDeviceDistributionApi = function () {
    return instance({
        method: 'get',
        url: `index/device/distribution`,
        headers: { isLoading: true },
    })
}
// 测点分布
export const indexSensorDistributionApi = function () {
    return instance({
        method: 'get',
        url: `/index/sensor/distribution`,
        headers: { isLoading: true },
    })
}
// 设备数据，地图展示
export const indexDeviceDataApi = function () {
    return instance({
        method: 'get',
        url: 'index/device/data',
        headers: { isLoading: true },
    })
}
// 查询区域下的设备列表
export const zoneGetlistApi = function (zone_id) {
    return instance({
        method: 'get',
        url: '/device/zone/getlist?zone_id=' + zone_id,
        headers: { isLoading: true },
    })
}
// 新增告警与工单
export const indexNewdataApi = function () {
    return instance({
        method: 'get',
        url: '/index/newdata',
        headers: { isLoading: false },
    })
}
// 近期告警
export const indexRecentAlarmApi = function () {
    // return instance.get("index/recent/alarm")
    return instance({
        method: 'get',
        url: 'index/recent/alarm',
        headers: { isLoading: true },
    })
}

//mqtt
export const getmqttApi = function () {
    return instance({ method: 'get', url: 'index/get/mqtt', headers: { isLoading: true } })
}
/**
 * 维修记录
 */
// 单条维修记录
export const deviceRepairlogListByDeviceApi = function (obj) {
    return instance({
        method: 'post',
        url: 'device_repairlog/listByDevice',
        data: obj,
        headers: { isLoading: true },
    })
}
/**
 * 后台节点
 */
// 获取后台节点树形结构
export const backstageTreeListApi = function (id) {
    if (id) return instance.get('backstage/tree/list?id=' + id)
    else return instance.get('backstage/tree/list')
}
// 获取后台节点
export const backstageGetlistApi = function (size, page, type) {
    return instance.get(`backstage/getlist?size=${size}&page=${page}&type=${type}`)
}
// 获取所有后台节点
export const backstageGetAllApi = function () {
    return instance.get('backstage/getAll')
}
// 添加节点
export const backstageAddApi = function (obj) {
    return instance.post('backstage/add', obj)
}
// 删除节点
export const backstageDeleteApi = function (id) {
    return instance.get('backstage/delete?id=' + id)
}
//修改节点
export const backstageChangeApi = function (obj) {
    return instance.post('backstage/change', obj)
}
//获取节点的 父级节点列表
export const backstageGetParentApi = function (id) {
    return instance.get('backstage/get/parent?id=' + id)
}

// 获取城市列表
export const cityListApi = function () {
    return instance.get('backstage/city/list')
}
//获取日志
export const logListApi = function (data) {
    return instance({
        method: 'post',
        url: 'log/list',
        headers: { isLoading: true },
        data,
    })
}
/**
 * 基础配置
 */
// 设备类型
export const deviceTypeGetlisApi = function (size, page) {
    return instance.get(`deviceType/getlist?size=${size}&page=${page}`)
}
// 特征值类型
export const getEigenvalueTagsApi = function () {
    return instance.post(`eigenvalue/getlist`, {})
}
export const getWaveTagsApi = function () {
    return instance.get(`eigenvalue/wave/list`)
}
// 传感器类型
export const sensorTypeGetlistApi = function (size, page) {
    return instance.get(`/sensorType/getlist?size=${size}&page=${page}`)
}
export const sensorTypeAddApi = function (obj) {
    return instance.post('sensorType/add', obj)
}
export const sensorTypeDeleteApi = function (obj) {
    return instance.post('sensorType/delete', obj)
}
export const sensorTypeEditApi = function (obj) {
    return instance.post('sensorType/edit', obj)
}
// 故障类型
export const troubleGetlistApi = function (size, page) {
    // return instance.get(`trouble/getlist?size=${size}&page=${page}`)
    return instance({
        method: 'get',
        url: `trouble/getlist?size=${size}&page=${page}`,
        headers: { isLoading: true },
    })
}

//巡检频率
export const cycleListApi = function () {
    return instance.get('point/cycle/list')
}

//查询固定阈值列表
export const sthredGetlistApi = function (obj) {
    // return instance.post('sthred/getlist', obj)
    return instance({
        method: 'post',
        url: 'sthred/getlist',
        data: obj,
        headers: { isLoading: true },
    })
}
//修改阈值
export const sthredChangeApi = function (obj) {
    return instance.post('sthred/change', obj)
}
// 设备配置，查询设备列表
export const setDeviceGetlistApi = function (obj) {
    return instance({
        method: 'post',
        url: 'set/device/getlist',
        data: obj,
        headers: { isLoading: true },
    })
}
/**
 * 设备相关
 */
//设备列表
export const devicegetlistApi = function (size, page) {
    // return instance.get(`device/getlist?page=${page}&size=${size}`)
    return instance({
        method: 'get',
        url: `device/getlist?page=${page}&size=${size}`,
        headers: { isLoading: true },
    })
}
// 设备详情
export const deviceDetailsApi = function (id) {
    return instance({
        method: 'get',
        url: `device/details?id=${id}`,
        headers: { isLoading: true },
    })
}
export const devicePageDetailsApi = function (id) {
    return instance({
        method: 'get',
        url: `device/getdetails?id=${id}`,
        headers: { isLoading: true },
    })
}
export const deviceGetSensorsApi = function (id) {
    return instance({
        method: 'get',
        url: `device/getSensors?id=${id}`,
        headers: { isLoading: true },
    })
}
//添加设备
export const deviceAdd = function (obj) {
    return instance.post('device/add', obj)
}
// 修改设备
export const deviceChange = function (data) {
    return instance({
        method: 'post',
        url: `device/change`,
        data,
        headers: { isLoading: true },
    })
}
// 上传设备2.5D图
export const deviceUpload25Dapi = function (obj) {
    return instance.post('device/upload/25D', obj)
}
export const deviceGetDevice25dApi = function (id) {
    return instance({
        method: 'get',
        url: '/device/getDevice25d/pc?id=' + id,
        headers: { isLoading: true },
    })
}
// 修改设备基础信息
export const deviceNameplateEditApi = function (data) {
    return instance({
        method: 'post',
        url: '/device/nameplate/edit',
        data,
        headers: { isLoading: true },
    })
}
export const deleteDeviceApi = function (data) {
    return instance({ method: 'post', url: '/device/delete', data })
}
/**
 * 测点相关
 */
//查询测点详情
export const sensorDetailsApi = function (id) {
    return instance({
        method: 'get',
        url: 'sensor/details?id=' + id,
        headers: { isLoading: true },
    })
}
//查询测点树形结构
export const sensorTreeApi = function (id) {
    // return instance.get('sensor/tree/getlist?id=' + id)
    return instance({
        method: 'get',
        url: 'sensor/tree/getlist?id=' + id,
        headers: { isLoading: false },
    })
}
// 保存测点位置信息
export const deviceSaveGraphApi = function (data) {
    return instance({
        method: 'post',
        url: 'device/save/graph',
        data,
        headers: { isLoading: true },
    })
}
// 根据设备ID查询测点最新特征值数据
export const getSensorEigensByDeviceApi = function (obj) {
    return instance({
        method: 'post',
        url: 'sensor/getSensorEigensByDevice',
        data: obj,
        headers: { isLoading: true },
    })
}
/**
 * 波形相关
 */
// 获取指定测点的特征值标签
export const eigenvalueTagsApi = function (id) {
    return instance({
        method: 'get',
        url: 'eigenvalue/tags?id=' + id,
        headers: { isLoading: false },
    })
}
// 获取测点某时间段的特征值
export const eigenvalueSensorGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'eigenvalue/sensor/getlist',
        data,
        headers: { isLoading: false },
    })
}

// 最近500点特征值数据
export const eigenvalueRecentApi = function (data) {
    return instance({
        method: 'post',
        url: 'eigenvalue/recent',
        data,
        headers: { isLoading: true },
    })
}

// 获取图谱标签数组(测点id)
export const specSpectralTagsApi = function (id) {
    return instance({
        method: 'post',
        url: 'spec/spectral/tags?id=' + id,
        headers: { isLoading: false },
    })
}
//获取测点的时间数组 图谱分析
export const specTimeGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/time/getlist',
        data,
        headers: { isLoading: false },
    })
}
//获取测点在一时间段内的数据
export const specDataGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/domain/listbyfeature',
        data,
        headers: { isLoading: false },
    })
}
// 获取图谱分析 数据
export const specDomainGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/domain/getlist',
        data,
        headers: { isLoading: false },
    })
}
export const domainTagsListApi = function () {
    return instance({
        method: 'get',
        url: 'spec/domain/tags/list',
    })
}
// 根据特征值时间节点查询出最近的图谱节点
export const specRecentTimeApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/recent/time',
        data,
        headers: { isLoading: true },
    })
}
// 获取波形特征值标签
export const eigenvalueWaveTagsApi = function (id) {
    return instance({
        method: 'post',
        url: 'eigenvalue/wave/tags?id=' + id,
        headers: { isLoading: true },
    })
}
// 获取波形特征值数据
export const eigenvalueWaveGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'eigenvalue/wave/getlist',
        data,
        headers: { isLoading: true },
    })
}

// 获取波形特征值时间数组
export const eigenvalueWaveTimelistApi = function (data) {
    return instance({
        method: 'post',
        url: 'eigenvalue/wave/timelist',
        data,
        headers: { isLoading: true },
    })
}
// 轨迹图
export const specDispalcementwaveApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/dispalcementwave',
        data,
        headers: { isLoading: true },
    })
}
//请选择 波形 和 频谱的 标签
export const domaintagsApi = function (data) {
    return instance({
        method: 'post',
        url: 'spec/domain/tags',
        data,
        headers: { isLoading: false },
    })
}
/**
 * 工厂详情界面
 *  */
//数据统计
export const factorytotalDataApi = function (id) {
    return instance({
        method: 'get',
        url: 'index/stats?id=' + id,
        headers: { isLoading: true },
    })
}
// 近期告警
export const factoryRecentAlarmApi = function (id) {
    return instance({
        method: 'get',
        url: 'index/recent/alarm?id=' + id,
        headers: { isLoading: true },
    })
}
// 区域数据
export const indexZonesApi = function (id) {
    return instance({
        method: 'get',
        url: 'index/zones?id=' + id,
        headers: { isLoading: true },
    })
}

/**
 * 告警与工单
 *
 */
// 查询告警列表
export const warnWarnlistApi = function (obj) {
    return instance({
        method: 'post',
        url: 'warn/warnlist',
        data: obj,
        headers: {
            isLoading: true,
        },
    })
}
//删除告警
export const warnDeleteApi = function (id) {
    return instance.get('warn/delete?id=' + id)
}
// 生成工单
export const warnCreateNotifyApi = function (obj) {
    return instance.post('warn/create/notify', obj)
}
// 查询工单列表
export const orderGetlistApi = function (data) {
    return instance({
        method: 'post',
        url: 'order/getlist',
        data,
        headers: {
            isLoading: true,
        },
    })
}
// 删除工单
export const orderDeleteApi = function (id) {
    return instance.get('order/delete?id=' + id)
}
//查询出可推送 工单 的用户列表
export const warnPullListApi = function (obj) {
    return instance.post('warn/pull/list', obj)
}
// 修改工单
export const orderChangeApi = function (obj) {
    return instance.post('order/change', obj)
}
// 查询 诊断 报告
export const reportGetlistApi = function (data) {
    return instance({ url: 'report/getlist', method: 'post', data, headers: { isLoading: true } })
}
//添加诊断报告
export const reportAddApi = function (obj) {
    return instance.post('report/add', obj)
}
//删除诊断报告
export const reportDeleteApi = function (id) {
    return instance.get('report/delete?id=' + id)
}

//自诊断记录
export const selfreportGetlistApi = function (data) {
    // return instance.post("selfreport/getlist", obj)
    return instance({
        method: 'post',
        url: 'selfreport/getlist',
        data,
        headers: {
            isLoading: true,
        },
    })
}
//导出
export const exportExcel = function (url, obj) {
    return instance({
        url, // 替换为实际的下载地址
        method: 'POST',
        responseType: 'blob', // 设置响应类型为 blob
        data: obj,
    })
        .then((response) => {
            // 创建一个临时的 URL 对象
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', obj.fileName) // 替换为要保存的文件名
            // 将 <a> 标签添加到文档中并触发点击事件
            document.body.appendChild(link)

            link.click()
            // 清理临时 URL 对象
            window.URL.revokeObjectURL(url)
            return Promise.resolve({
                code: 1,
                msg: '下载成功',
            })
        })
        .catch((error) => {
            console.error('下载失败:', error)
        })
}
// 下载诊断报告
export const reportDownloadApi = function (fileName) {
    return instance({
        url: 'report/download/' + fileName,
        method: 'post',
        responseType: 'blob',
    })
        .then((response) => {
            // 创建一个临时的 URL 对象
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', fileName) // 替换为要保存的文件名
            // 将 <a> 标签添加到文档中并触发点击事件
            document.body.appendChild(link)

            link.click()
            // 清理临时 URL 对象
            window.URL.revokeObjectURL(url)
            return Promise.resolve({
                code: -1,
                msg: '下载成功',
            })
        })
        .catch((error) => {
            console.error('下载失败:', error)
        })
}
// 预览
export const reportViewpdfApi = function (fileName) {
    return instance({
        url: 'report/viewpdf/' + fileName,
        method: 'post',
        responseType: 'blob',
    })
        .then((response) => {
            // 创建一个临时的 URL 对象
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', fileName) // 替换为要保存的文件名
            // 将 <a> 标签添加到文档中并触发点击事件
            document.body.appendChild(link)
            link.click()
            // 清理临时 URL 对象
            window.URL.revokeObjectURL(url)
            return Promise.resolve({
                code: -1,
                msg: '下载成功',
            })
        })
        .catch((error) => {
            console.error('下载失败:', error)
        })
}

export const downloadFile = function (name,url) {
    return instance({
        url: 'store/download',
        data: {
            name,
            url
        },
        method: 'post',
        responseType: 'blob',
    })
        .then((response) => {
            // 创建一个临时的 URL 对象
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', name) // 替换为要保存的文件名
            // 将 <a> 标签添加到文档中并触发点击事件
            document.body.appendChild(link)
            link.click()
            // 清理临时 URL 对象
            window.URL.revokeObjectURL(url)
            return Promise.resolve({
                code: -1,
                msg: '下载成功',
            })
        })
        .catch((error) => {
            console.error('下载失败:', error)
        })
}

/**
 * 数据库管理
 */
// 分页列表
export const dbmanageListApi = function (data) {
    return instance({ method: 'post', headers: { isLoading: true }, data, url: 'dbmanage/list' })
}
// 新增
export const dbmanageAddApi = function (data) {
    return instance({ method: 'post', headers: { isLoading: true }, data, url: 'dbmanage/add' })
}
//编辑
export const dbmanagechangeApi = function (data) {
    return instance({ method: 'post', headers: { isLoading: true }, data, url: 'dbmanage/change' })
}
//删除
export const dbmanageDeleteApi = function (id) {
    return instance({ method: 'post', headers: { isLoading: true }, data: { id }, url: 'dbmanage/delete' })
}
/**
 * 巡检记录
 */
// 查询
export const recordlistApi = function (data) {
    return instance({ method: 'post', data, url: 'point/record/getlist', headers: { isLoading: true } })
}
export const recordAddApi = function (data) {
    return instance({ method: 'post', data, url: 'point/record/add', headers: { isLoading: true } })
}
//删除
export const recordDeleteApi = function (id) {
    return instance.get('point/record/delete?id=' + id)
}
//修改
export const recordSetApi = function (data) {
    return instance({ method: 'post', data, url: 'point/record/set' })
}
/**
 * 巡检计划
 * */
export const planListApi = function (data) {
    return instance({ method: 'post', data, url: 'point/plan/list', headers: { isLoading: true } })
}
//添加
export const planAddApi = function (data) {
    return instance({ method: 'post', data, url: 'point/plan/add', headers: { isLoading: true } })
}
//删除
export const planDeleteApi = function (data) {
    return instance({ method: 'post', data, url: 'point/plan/delete', headers: { isLoading: true } })
}
// 停止
export const planStopApi = function (data) {
    return instance({ method: 'post', data, url: 'point/plan/stop' })
}
//修改
export const planSetApi = function (data) {
    return instance({ method: 'post', data, url: 'point/plan/set' })
}
//通过测点类型获取 特征值 数组
export const getEigApi = function (data) {
    return instance({ method: 'post', data, url: 'sensorType/get/eig' })
}

export const requestApi = function (method, url, data = {}, isLoading = false) {
    return instance({
        method,
        url,
        data,
        headers: { isLoading },
    })
}
export const fileApi = function (method, url, obj, isLoading = false) {
    return instance({
        url, // 替换为实际的下载地址
        method,
        responseType: 'blob', // 设置响应类型为 blob
        data: obj,
        headers: { isLoading },
    })
        .then((response) => {
            // 获取文件名
            var contentDisposition = response.headers['content-disposition']

            try {
                var fileName = decodeURIComponent(contentDisposition.match(/filename\*=UTF-8''([^;]*)/)[1]) || contentDisposition.match(/filename="([^;]*)"/)[1]
            } catch {
                var fileName = '未命名文件.txt'
            }
            // 创建一个临时的 URL 对象
            const url = window.URL.createObjectURL(new Blob([response.data]))
            const link = document.createElement('a')
            link.href = url

            link.setAttribute('download', fileName) // 替换为要保存的文件名
            // 将 <a> 标签添加到文档中并触发点击事件
            document.body.appendChild(link) + -link.click()
            // 清理临时 URL 对象
            window.URL.revokeObjectURL(url)
            return Promise.resolve({
                code: 0,
                msg: '操作成功',
            })
        })
        .catch((error) => {
            console.error('操作失败:', error)
        })
}
